package com.smart.community.region.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.smart.community.commons.utils.SecurityUtils;
import com.smart.community.region.dto.ImportRequest;
import com.smart.community.region.service.ICacheService;
import com.smart.community.region.service.ICommunityTemplateImportService;
import com.smart.community.region.service.IDataImportService;
import com.smart.community.region.utils.DataValidationUtils;
import com.smart.community.region.utils.ExcelTemplateUtils;
import com.smart.community.region.vo.ImportProgress;
import com.smart.community.region.vo.ImportResult;
import com.smart.community.region.vo.TemplateValidationResult;

import lombok.extern.slf4j.Slf4j;

/**
 * 社区模板导入服务实现类
 * 
 * @author Wu.Liang
 * @since 2025-01-30
 */
@Slf4j
@Service
public class CommunityTemplateImportServiceImpl implements ICommunityTemplateImportService {

    @Autowired
    private ExcelTemplateUtils excelTemplateUtils;
    
    @Autowired
    private DataValidationUtils dataValidationUtils;
    
    @Autowired
    private IDataImportService dataImportService;
    
    @Autowired
    private ICacheService cacheService;

    @Override
    public void downloadTemplate(HttpServletResponse response) {
        long startTime = System.currentTimeMillis();
        log.info("开始下载社区模板文件");
        try {
            // 参数验证
            if (response == null) {
                log.warn("响应对象为空，无法下载模板");
                return;
            }
            
            // 缓存键
            String cacheKey = "template:community:excel";
            
            // 尝试从缓存获取模板数据
            byte[] templateData = cacheService.getCachedTemplateBytes(cacheKey);
            
            if (templateData == null) {
                log.info("缓存中未找到模板数据，开始生成新的模板文件");
                // 生成Excel模板文件
                templateData = excelTemplateUtils.generateCommunityTemplate();
                
                // 缓存模板数据，过期时间24小时
                templateData = cacheService.cacheTemplateBytes(cacheKey, templateData, 1440);
                log.info("模板文件生成成功并已缓存，缓存键：{}", cacheKey);
            } else {
                log.info("从缓存获取模板数据成功，缓存键：{}", cacheKey);
            }
            
            // 设置响应头
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''%E7%A4%BE%E5%8C%BA%E6%A8%A1%E6%9D%BF%E5%AF%BC%E5%85%A5.xlsx");
            response.setContentLength(templateData.length);
            
            // 输出到响应流
            response.getOutputStream().write(templateData);
            response.getOutputStream().flush();
            
            long endTime = System.currentTimeMillis();
            log.info("社区模板文件下载成功，耗时：{}ms", endTime - startTime);
        } catch (Exception e) {
            long endTime = System.currentTimeMillis();
            log.error("下载社区模板文件失败，耗时：{}ms", endTime - startTime, e);
            // 生产环境优雅降级，不抛异常
        }
    }


    @Override
    public String uploadTemplate(MultipartFile file) {
        long startTime = System.currentTimeMillis();
        log.info("开始处理模板文件上传，文件名：{}", file != null ? file.getOriginalFilename() : "null");
        try {
            // 参数验证
            if (file == null) {
                log.warn("上传文件为空，返回null");
                return null;
            }
            
            // 验证文件格式
            validateFileFormat(file);
            
            // 验证文件大小
            validateFileSize(file);
            
            // 生成文件ID
            String fileId = generateFileId();
            
            // 解析Excel文件内容
            Map<String, List<Map<String, Object>>> parsedData = excelTemplateUtils.parseExcelFile(file);
            
            // 缓存解析结果，过期时间2小时
            cacheService.cacheTemplateData(fileId, parsedData, 120);
            
            long endTime = System.currentTimeMillis();
            log.info("模板文件上传处理完成，文件ID：{}，解析数据：楼栋{}条，单元{}条，房户{}条，耗时：{}ms", 
                    fileId, 
                    parsedData.get("buildings").size(),
                    parsedData.get("units").size(),
                    parsedData.get("households").size(),
                    endTime - startTime);
            return fileId;
        } catch (Exception e) {
            long endTime = System.currentTimeMillis();
            log.error("处理模板文件上传失败，耗时：{}ms", endTime - startTime, e);
            return null; // 生产环境优雅降级
        }
    }

    @Override
    public TemplateValidationResult validateTemplate(String fileId) {
        long startTime = System.currentTimeMillis();
        log.info("开始验证模板数据，文件ID：{}", fileId);
        try {
            // 参数验证
            if (fileId == null || fileId.trim().isEmpty()) {
                log.warn("文件ID为空，返回null");
                return null;
            }
            
            // 从缓存获取解析的文件数据
            Map<String, List<Map<String, Object>>> parsedData = cacheService.getCachedTemplateData(fileId);
            if (parsedData == null) {
                log.warn("文件数据不存在或已过期，文件ID：{}", fileId);
                return null;
            }
            
            // 执行数据验证
            TemplateValidationResult result = performDataValidation(parsedData, 1L);
            
            // 缓存验证结果，过期时间30分钟
            cacheService.cacheValidationResult(fileId, result, 30);
            
            long endTime = System.currentTimeMillis();
            log.info("模板数据验证完成，文件ID：{}，验证结果：{}，耗时：{}ms", fileId, result.getIsValid() ? "有效" : "无效", endTime - startTime);
            return result;
        } catch (Exception e) {
            long endTime = System.currentTimeMillis();
            log.error("验证模板数据失败，文件ID：{}，耗时：{}ms", fileId, endTime - startTime, e);
            return null; // 生产环境优雅降级
        }
    }
    
    /**
     * 执行数据验证
     */
    private TemplateValidationResult performDataValidation(
            Map<String, List<Map<String, Object>>> parsedData, 
            Long communityId) {
        
        TemplateValidationResult result = new TemplateValidationResult();
        List<TemplateValidationResult.ValidationError> allErrors = new ArrayList<>();
        
        // 获取现有数据（实际应该从数据库查询）
        Set<String> existingBuildingNos = new HashSet<>();
        Set<String> existingHouseholdCodes = new HashSet<>();
        
        // 验证楼栋数据
        List<Map<String, Object>> buildingData = parsedData.get("buildings");
        DataValidationUtils.ValidationResult buildingValidation = dataValidationUtils.validateBuildingData(buildingData, communityId);
        List<TemplateValidationResult.ValidationError> buildingErrors = convertValidationErrors(buildingValidation.getErrors());
        allErrors.addAll(buildingErrors);
        
        // 构建楼栋映射
        Map<String, Map<String, Object>> buildingMap = new HashMap<>();
        for (Map<String, Object> building : buildingData) {
            String buildingNo = (String) building.get("buildingNo");
            buildingMap.put(buildingNo, building);
        }
        
        // 验证单元数据
        List<Map<String, Object>> unitData = parsedData.get("units");
        DataValidationUtils.ValidationResult unitValidation = dataValidationUtils.validateUnitData(unitData, buildingData, communityId);
        List<TemplateValidationResult.ValidationError> unitErrors = convertValidationErrors(unitValidation.getErrors());
        allErrors.addAll(unitErrors);
        
        // 构建单元映射
        Map<String, Map<String, Object>> unitMap = new HashMap<>();
        for (Map<String, Object> unit : unitData) {
            String buildingNo = (String) unit.get("buildingNo");
            Integer unitNo = (Integer) unit.get("unitNo");
            unitMap.put(buildingNo + "-" + unitNo, unit);
        }
        
        // 验证房户数据
        List<Map<String, Object>> householdData = parsedData.get("households");
        DataValidationUtils.ValidationResult householdValidation = dataValidationUtils.validateHouseholdData(householdData, buildingData, unitData, communityId);
        List<TemplateValidationResult.ValidationError> householdErrors = convertValidationErrors(householdValidation.getErrors());
        allErrors.addAll(householdErrors);
        
        // 设置验证结果
        result.setIsValid(allErrors.isEmpty());
        result.setErrors(allErrors);
        
        // 设置数据统计
        TemplateValidationResult.DataStatistics buildingStats = new TemplateValidationResult.DataStatistics();
        buildingStats.setTotalCount(buildingData.size());
        buildingStats.setValidCount(buildingData.size() - (int) buildingErrors.stream().map(TemplateValidationResult.ValidationError::getRowNumber).distinct().count());
        buildingStats.setErrorCount((int) buildingErrors.stream().map(TemplateValidationResult.ValidationError::getRowNumber).distinct().count());
        buildingStats.setDuplicateCount(0);
        result.setBuildingStats(buildingStats);
        
        TemplateValidationResult.DataStatistics unitStats = new TemplateValidationResult.DataStatistics();
        unitStats.setTotalCount(unitData.size());
        unitStats.setValidCount(unitData.size() - (int) unitErrors.stream().map(TemplateValidationResult.ValidationError::getRowNumber).distinct().count());
        unitStats.setErrorCount((int) unitErrors.stream().map(TemplateValidationResult.ValidationError::getRowNumber).distinct().count());
        unitStats.setDuplicateCount(0);
        result.setUnitStats(unitStats);
        
        TemplateValidationResult.DataStatistics householdStats = new TemplateValidationResult.DataStatistics();
        householdStats.setTotalCount(householdData.size());
        householdStats.setValidCount(householdData.size() - (int) householdErrors.stream().map(TemplateValidationResult.ValidationError::getRowNumber).distinct().count());
        householdStats.setErrorCount((int) householdErrors.stream().map(TemplateValidationResult.ValidationError::getRowNumber).distinct().count());
        householdStats.setDuplicateCount(0);
        result.setHouseholdStats(householdStats);
        
        return result;
    }

    @Override
    public ImportResult importTemplate(ImportRequest request) {
        long startTime = System.currentTimeMillis();
        log.info("开始导入模板数据，文件ID：{}，社区ID：{}", request != null ? request.getFileId() : "null", request != null ? request.getCommunityId() : "null");
        
        // 生成任务ID（无论成功失败都要生成，确保前端能获取到taskId）
        String taskId = generateTaskId();
        
        try {
            // 参数验证
            if (request == null) {
                log.warn("导入请求为空，任务ID：{}", taskId);
                return handleParameterValidationFailure(taskId, "导入请求为空");
            }
            
            if (request.getFileId() == null || request.getFileId().trim().isEmpty()) {
                log.warn("文件ID为空，任务ID：{}", taskId);
                return handleParameterValidationFailure(taskId, "文件ID为空");
            }
            
            if (request.getCommunityId() == null) {
                log.warn("社区ID为空，任务ID：{}", taskId);
                return handleParameterValidationFailure(taskId, "社区ID为空");
            }
            
            // 从缓存获取解析的文件数据
            Map<String, List<Map<String, Object>>> parsedData = cacheService.getCachedTemplateData(request.getFileId());
            if (parsedData == null) {
                log.warn("文件数据不存在或已过期，文件ID：{}，任务ID：{}", request.getFileId(), taskId);
                return handleParameterValidationFailure(taskId, "文件数据不存在或已过期");
            }
            
            // 执行数据导入
            ImportResult result = performDataImport(parsedData, request.getCommunityId(), taskId, request.getOverwrite(), request.getSkipErrors());
            
            // 注意：performDataImport方法内部已经缓存了ImportProgress对象
            // 这里不需要再次缓存ImportResult对象
            
            long endTime = System.currentTimeMillis();
            log.info("模板数据导入任务已启动，任务ID：{}，耗时：{}ms", taskId, endTime - startTime);
            return result;
        } catch (Exception e) {
            long endTime = System.currentTimeMillis();
            log.error("导入模板数据失败，文件ID：{}，任务ID：{}，耗时：{}ms", request != null ? request.getFileId() : "null", taskId, endTime - startTime, e);
            
            // 创建失败的导入结果，确保前端能获取到taskId
            ImportResult failedResult = createFailedImportResult(taskId, "导入失败：" + e.getMessage());
            
            // 创建失败的进度对象并缓存
            ImportProgress failedProgress = new ImportProgress();
            failedProgress.setTaskId(taskId);
            failedProgress.setStatus("FAILED");
            failedProgress.setProgress(100);
            failedProgress.setCurrentStep("导入失败");
            failedProgress.setErrorMessage("导入失败：" + e.getMessage());
            failedProgress.setStartTime(LocalDateTime.now());
            failedProgress.setEndTime(LocalDateTime.now());
            failedProgress.setTotalSteps(4);
            failedProgress.setCurrentStepNumber(4);
            failedProgress.setProcessedCount(0);
            failedProgress.setTotalCount(0);
            failedProgress.setErrorCount(1);
            
            // 缓存失败的进度对象
            cacheService.cacheImportProgress(taskId, failedProgress, 60);
            
            return failedResult;
        }
    }
    
    /**
     * 执行数据导入
     */
    private ImportResult performDataImport(Map<String, List<Map<String, Object>>> parsedData, 
                                          Long communityId, String taskId, Boolean overwrite, Boolean skipErrors) {
        LocalDateTime startTime = LocalDateTime.now();
        
        try {
            // 获取当前用户ID
            Long userId = SecurityUtils.getCurrentUserId();
            
            // 初始化进度
            ImportProgress progress = new ImportProgress();
            progress.setTaskId(taskId);
            progress.setStatus("PROCESSING");
            progress.setProgress(0);
            progress.setCurrentStep("开始导入");
            progress.setTotalSteps(4);
            progress.setCurrentStepNumber(1);
            progress.setStartTime(startTime);
            progress.setProcessedCount(0);
            progress.setTotalCount(0);
            progress.setErrorCount(0);
            
            // 缓存初始进度
            cacheService.cacheImportProgress(taskId, progress, 60);
            
            // 更新进度：数据验证
            progress.setCurrentStep("数据验证中");
            progress.setProgress(25);
            progress.setCurrentStepNumber(2);
            cacheService.cacheImportProgress(taskId, progress, 60);
            
            // 使用数据导入服务进行导入
            DataImportServiceImpl.ImportResult importResult = dataImportService.importCommunityData(parsedData, communityId, userId, overwrite, skipErrors);
            
            if (!importResult.isSuccess()) {
                // 更新进度：导入失败
                progress.setStatus("FAILED");
                progress.setProgress(100);
                progress.setCurrentStep("导入失败");
                progress.setErrorMessage(importResult.getMessage());
                cacheService.cacheImportProgress(taskId, progress, 60);
                
                throw new RuntimeException(importResult.getMessage());
            }
            
            int buildingCount = importResult.getBuildingCount();
            int unitCount = importResult.getUnitCount();
            int householdCount = importResult.getHouseholdCount();
            
            // 更新进度：导入完成
            progress.setStatus("COMPLETED");
            progress.setProgress(100);
            progress.setCurrentStep("导入完成");
            progress.setCurrentStepNumber(4);
            progress.setProcessedCount(buildingCount + unitCount + householdCount);
            progress.setTotalCount(buildingCount + unitCount + householdCount);
            progress.setEstimatedEndTime(LocalDateTime.now());
            cacheService.cacheImportProgress(taskId, progress, 60);
            
            // 构建导入结果
            LocalDateTime endTime = LocalDateTime.now();
            ImportResult result = new ImportResult();
            result.setTaskId(taskId);
            result.setSuccess(true);
            result.setBuildingCount(buildingCount);
            result.setUnitCount(unitCount);
            result.setHouseholdCount(householdCount);
            result.setErrorCount(0);
            result.setStartTime(startTime);
            result.setEndTime(endTime);
            result.setDuration(java.time.Duration.between(startTime, endTime).toMillis());
            
            log.info("数据导入完成，楼栋：{}，单元：{}，房户：{}", buildingCount, unitCount, householdCount);
            return result;
            
        } catch (Exception e) {
            log.error("数据导入失败", e);
            
            // 更新进度：导入失败
            ImportProgress progress = new ImportProgress();
            progress.setTaskId(taskId);
            progress.setStatus("FAILED");
            progress.setProgress(100);
            progress.setCurrentStep("导入失败");
            progress.setErrorMessage(e.getMessage());
            progress.setStartTime(startTime);
            progress.setEndTime(LocalDateTime.now());
            cacheService.cacheImportProgress(taskId, progress, 60);
            
            ImportResult result = new ImportResult();
            result.setTaskId(taskId);
            result.setSuccess(false);
            result.setErrorMessage(e.getMessage());
            result.setStartTime(startTime);
            result.setEndTime(LocalDateTime.now());
            return result;
        }
    }
    
    /**
     * 构建楼栋ID映射
     */
    private Map<String, Long> buildBuildingIdMap(List<Map<String, Object>> buildingData, Long communityId) {
        // 实际应该从数据库查询楼栋ID
        // 这里需要根据楼栋编号查询楼栋ID
        Map<String, Long> buildingIdMap = new HashMap<>();
        for (int i = 0; i < buildingData.size(); i++) {
            String buildingNo = (String) buildingData.get(i).get("buildingNo");
            buildingIdMap.put(buildingNo, (long) (i + 1)); // 模拟ID
        }
        return buildingIdMap;
    }
    
    /**
     * 构建单元ID映射
     */
    private Map<String, Long> buildUnitIdMap(List<Map<String, Object>> unitData, Map<String, Long> buildingIdMap) {
        // 实际应该从数据库查询单元ID
        // 这里需要根据单元编号查询单元ID
        Map<String, Long> unitIdMap = new HashMap<>();
        for (int i = 0; i < unitData.size(); i++) {
            String buildingNo = (String) unitData.get(i).get("buildingNo");
            Integer unitNo = (Integer) unitData.get(i).get("unitNo");
            unitIdMap.put(buildingNo + "-" + unitNo, (long) (i + 1)); // 模拟ID
        }
        return unitIdMap;
    }
    
    /**
     * 获取模拟数据
     */
    private Map<String, List<Map<String, Object>>> getMockData() {
        Map<String, List<Map<String, Object>>> parsedData = new HashMap<>();
        
        // 模拟楼栋数据
        List<Map<String, Object>> buildingData = new ArrayList<>();
        Map<String, Object> building1 = new HashMap<>();
        building1.put("buildingNo", "A1");
        building1.put("buildingName", "A1栋");
        building1.put("floorCount", 18);
        building1.put("unitCount", 2);
        building1.put("unitsPerFloor", 4);
        buildingData.add(building1);
        parsedData.put("buildings", buildingData);
        
        // 模拟单元数据
        List<Map<String, Object>> unitData = new ArrayList<>();
        Map<String, Object> unit1 = new HashMap<>();
        unit1.put("buildingNo", "A1");
        unit1.put("unitNo", 1);
        unit1.put("unitName", "1单元");
        unit1.put("floorCount", 18);
        unit1.put("roomCount", 72);
        unitData.add(unit1);
        parsedData.put("units", unitData);
        
        // 模拟房户数据
        List<Map<String, Object>> householdData = new ArrayList<>();
        Map<String, Object> household1 = new HashMap<>();
        household1.put("buildingNo", "A1");
        household1.put("unitNo", 1);
        household1.put("householdCode", "A1-1-101");
        household1.put("roomNumber", "101");
        household1.put("floorNumber", 1);
        household1.put("area", new BigDecimal("89.5"));
        householdData.add(household1);
        parsedData.put("households", householdData);
        
        return parsedData;
    }

    @Override
    public ImportProgress getImportProgress(String taskId) {
        long startTime = System.currentTimeMillis();
        log.info("查询导入进度，任务ID：{}", taskId);
        try {
            // 参数验证
            if (taskId == null || taskId.trim().isEmpty()) {
                log.warn("任务ID为空，返回null");
                return null;
            }
            
            // 从缓存获取任务进度
            Object cachedProgress = cacheService.getCachedImportProgress(taskId);
            if (cachedProgress != null) {
                long endTime = System.currentTimeMillis();
                log.info("从缓存获取导入进度成功，任务ID：{}，耗时：{}ms", taskId, endTime - startTime);
                
                // 类型安全检查
                if (cachedProgress instanceof ImportProgress) {
                    return (ImportProgress) cachedProgress;
                } else {
                    log.warn("缓存中的对象类型不正确，期望ImportProgress，实际类型：{}，任务ID：{}", 
                            cachedProgress.getClass().getSimpleName(), taskId);
                    // 如果类型不匹配，返回默认进度
                }
            }
            
            // 如果缓存中没有，返回默认进度
            ImportProgress progress = new ImportProgress();
            progress.setTaskId(taskId);
            progress.setStatus("NOT_FOUND");
            progress.setProgress(0);
            progress.setCurrentStep("任务不存在");
            progress.setTotalSteps(4);
            progress.setCurrentStepNumber(0);
            progress.setStartTime(LocalDateTime.now());
            progress.setEstimatedEndTime(LocalDateTime.now());
            progress.setProcessedCount(0);
            progress.setTotalCount(0);
            progress.setErrorCount(0);
            progress.setErrorMessage("任务不存在或已过期");
            
            long endTime = System.currentTimeMillis();
            log.warn("导入进度不存在，任务ID：{}，耗时：{}ms", taskId, endTime - startTime);
            return progress;
        } catch (Exception e) {
            long endTime = System.currentTimeMillis();
            log.error("查询导入进度失败，任务ID：{}，耗时：{}ms", taskId, endTime - startTime, e);
            return null; // 生产环境优雅降级
        }
    }

    /**
     * 验证文件格式
     */
    private void validateFileFormat(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        if (originalFilename == null || !originalFilename.toLowerCase().endsWith(".xlsx")) {
            throw new RuntimeException("只支持.xlsx格式的Excel文件");
        }
    }

    /**
     * 验证文件大小
     */
    private void validateFileSize(MultipartFile file) {
        long maxSize = 10 * 1024 * 1024; // 10MB
        if (file.getSize() > maxSize) {
            throw new RuntimeException("文件大小不能超过10MB");
        }
    }

    /**
     * 生成文件ID
     */
    private String generateFileId() {
        return "FILE_" + System.currentTimeMillis() + "_" + UUID.randomUUID().toString().substring(0, 8);
    }

    /**
     * 生成任务ID
     */
    private String generateTaskId() {
        return "TASK_" + System.currentTimeMillis() + "_" + UUID.randomUUID().toString().substring(0, 8);
    }
    
    /**
     * 创建失败的导入结果
     * 
     * @param taskId 任务ID
     * @param errorMessage 错误信息
     * @return 失败的导入结果
     */
    private ImportResult createFailedImportResult(String taskId, String errorMessage) {
        ImportResult result = new ImportResult();
        result.setTaskId(taskId);
        result.setSuccess(false);
        result.setErrorMessage(errorMessage);
        result.setBuildingCount(0);
        result.setUnitCount(0);
        result.setHouseholdCount(0);
        result.setErrorCount(0);
        result.setStartTime(LocalDateTime.now());
        result.setEndTime(LocalDateTime.now());
        result.setDuration(0L);
        return result;
    }
    
    /**
     * 处理参数验证失败
     * 
     * @param taskId 任务ID
     * @param errorMessage 错误信息
     * @return 失败的导入结果
     */
    private ImportResult handleParameterValidationFailure(String taskId, String errorMessage) {
        // 创建失败的进度对象并缓存
        ImportProgress failedProgress = new ImportProgress();
        failedProgress.setTaskId(taskId);
        failedProgress.setStatus("FAILED");
        failedProgress.setProgress(100);
        failedProgress.setCurrentStep("参数验证失败");
        failedProgress.setErrorMessage(errorMessage);
        failedProgress.setStartTime(LocalDateTime.now());
        failedProgress.setEndTime(LocalDateTime.now());
        failedProgress.setTotalSteps(4);
        failedProgress.setCurrentStepNumber(1);
        failedProgress.setProcessedCount(0);
        failedProgress.setTotalCount(0);
        failedProgress.setErrorCount(1);
        
        // 缓存失败的进度对象
        cacheService.cacheImportProgress(taskId, failedProgress, 60);
        
        // 创建失败的导入结果
        return createFailedImportResult(taskId, errorMessage);
    }

    /**
     * 转换验证错误格式
     */
    private List<TemplateValidationResult.ValidationError> convertValidationErrors(List<DataValidationUtils.ValidationError> errors) {
        List<TemplateValidationResult.ValidationError> result = new ArrayList<>();
        for (DataValidationUtils.ValidationError error : errors) {
            TemplateValidationResult.ValidationError convertedError = new TemplateValidationResult.ValidationError();
            convertedError.setRowNumber(error.getRowIndex());
            convertedError.setColumnName(error.getField());
            convertedError.setErrorMessage(error.getMessage());
            convertedError.setErrorType("数据验证错误");
            convertedError.setSuggestion("请检查并修正数据");
            result.add(convertedError);
        }
        return result;
    }


}
